import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
import Utils from './../../../utils';
import { Control } from 'react-keeper';
import update from 'react-addons-update';
import DB from './../../../app/db';

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    ...PageConst,
    //操作类型
    operType: 'add',
    //操作类型
    isNew : true,

    //数据主键ID
    spId: '',
    //是否锁定
    readOnly: false,

    //是否上传
    isUpload: false,
    //选中的工程
    currentEng: {},
    //保存成功弹出提示
    showToptips: false,

    showWarn: false,
    warnTimer: null,
    codeTimer: null,
    warnMessage: '',

  },

  initMode: function () {
    let t = this;
    t.data.operType == 'add';

    this.data.jlmode = update(this.data.jlmode,  {$set: {}});
    this.data.jlmode.jlr = update(this.data.jlmode.jlr,  {$set: '13972608122'});
    this.data.jlmode.router = update(this.data.jlmode.router,  {$set: '/myyewu/rizhijl'});
    this.data.jlmode.jllxName = update(this.data.jlmode.jllxName,  {$set: '监理日志'});
    this.data.jlmode.jllx = update(this.data.jlmode.jllx,  {$set: '0071'});
    this.data.isNew = update(this.data.isNew,  {$set: true});
    this.data.isUpload = update(this.data.isUpload,  {$set: false});
    this.data.currentEng =  update(this.data.currentEng,  {$set: {}});

    var jlrq = new Date();
    let a = jlrq.getHours();
    if(a<10)
    {
      jlrq.setDate(jlrq.getDate()-1);//设置天数 -1 天
    }
    this.data.jlmode.jlrq = update(this.data.jlmode.jlrq,  {$set: jlrq.getTime()});


    DB.WechatAPI.getTianQi().then(
      function (content) {

        t.onTqqkChange(content.tqqk);
        t.onTqwdChange(content.qwqk);
        //t.onJlrqChange(content.jlrq)
      },
      function (error) {
        console.log(error);

      });
    t.updateComponent();
  },

  loadMode: function (params) {
    let t = this;
    this.data.jlmode = update(this.data.jlmode,  {$set: params});
    this.data.jlmode.id = update(this.data.jlmode.id,  {$set: params.id});
    this.data.currentEng =  update(this.data.currentEng,  {$set: params.eng});
    this.data.jlmode.sgjd = update(this.data.jlmode.sgjd,  {$set: params.sgjd});
    this.data.jlmode.tqqk = update(this.data.jlmode.tqqk,  {$set: params.tqqk});
    this.data.jlmode.qwqk = update(this.data.jlmode.qwqk,  {$set: params.qwqk});
    this.data.jlmode.sgjzqk = update(this.data.jlmode.sgjzqk,  {$set: params.sgjzqk});
    this.data.jlmode.jlgzqk = update(this.data.jlmode.jlgzqk,  {$set: params.jlgzqk});
    this.data.jlmode.czwt = update(this.data.jlmode.czwt,  {$set: params.czwt});
    this.data.jlmode.jlzl = update(this.data.jlmode.jlzl,  {$set: params.jlzl});
    this.data.jlmode.qzsx = update(this.data.jlmode.qzsx,  {$set: params.qzsx});
    this.data.isUpload = update(this.data.isUpload,  {$set: params.isUpload});
    this.data.isNew = update(this.data.isNew,  {$set: false});
    this.data.readOnly = false;
    this.data.operType = 'update';

    t.updateComponent();
  },

  showWarnMessage: function (message) {

    this.data.warnMessage = update(this.data.warnMessage, {$set: message});
    this.data.showWarn = update(this.data.showWarn, {$set: true});
    this.data.warnTimer = setTimeout(()=> {
      this.data.showWarn = update(this.data.showWarn, {$set: false});
      this.updateComponent();
    }, 3000);

    this.updateComponent();
  },

  onSetCurrentEng: function (obj) {
    this.data.currentEng = update(this.data.currentEng, {$set: obj});
    this.data.jlmode = update(this.data.jlmode,{eng : {$set: obj}}  );
    this.updateComponent();
  },

  onSaveChange: function (obj) {
    this.data.jlmode.id = update(this.data.jlmode.id,  {$set: obj});
    this.data.isNew = update(this.data.isNew,  {$set: false});
    this.data.readOnly = false;
    this.data.operType = 'update';
    this.updateComponent();
  },

  onSaveInfo: function (obj) {
    let t = this;
    let resultEng = Utils.isEmptyObject(this.data.currentEng);
    console.info(resultEng.success);
    if (!resultEng.success)
    {
      this.showWarnMessage('工程不能为空');
      return;
    }
    console.log('保存开始');
    DB.WechatAPI.saveJlrizhiInfo(this.data.jlmode).then(
      function (content) {
        //Control.go('/myyewu/rizhiaqlist');
        console.log('保存后');
        console.info(content);
        t.onSaveChange(content.code);
      },
      function (error) {
        console.log(error);
      });
    this.updateComponent();
  },


  onUpInfo: function (obj) {
    let resultEng = Utils.isEmptyObject(this.data.currentEng);

    if (!resultEng.success)
    {
      this.showWarnMessage('工程不能为空');
      return;
    }
    let sp = {id: this.data.jlmode.id};
    console.log('id');
    console.info(sp);
    DB.WechatAPI.upJlrizhiInfo(sp).then(
      function (content) {
        Control.go(-1);
      },
      function (error) {
        console.log(error);
      });
    this.updateComponent();
  },

  onJlrqChange: function (params) {

    this.data.jlmode.jlrq = update(this.data.jlmode.jlrq,  {$set: params});
    this.updateComponent();
  },


  onSgjdChange: function (params) {
    this.data.jlmode.sgjd = update(this.data.jlmode.sgjd,  {$set: params});
    this.updateComponent();
  },

  onTqqkChange: function (params) {
    this.data.jlmode.tqqk = update(this.data.jlmode.tqqk,  {$set: params});
    this.updateComponent();
  },

  onTqwdChange: function (params) {
    this.data.jlmode.qwqk = update(this.data.jlmode.qwqk,  {$set: params});
    this.updateComponent();
  },

  onQksgChange: function (params) {
    this.data.jlmode.sgjzqk = update(this.data.jlmode.sgjzqk,  {$set: params});
    console.info(this.data.jlmode);
    this.updateComponent();
  },

  onQkjlChange: function (params) {
    this.data.jlmode.jlgzqk = update(this.data.jlmode.jlgzqk,  {$set: params});
    console.info(this.data.jlmode);
    this.updateComponent();
  },

  onQkwtChange: function (params) {
    this.data.jlmode.czwt = update(this.data.jlmode.czwt,  {$set: params});
    console.info(this.data.jlmode);
    this.updateComponent();
  },

  onQklsChange: function (params) {
    this.data.jlmode.jlzl = update(this.data.jlmode.jlzl,  {$set: params});
    console.info(this.data.jlmode);
    this.updateComponent();
  },

  onQkqtChange: function (params) {
    this.data.jlmode.qzsx = update(this.data.jlmode.qzsx,  {$set: params});
    console.info(this.data.jlmode);
    this.updateComponent();
  },


  //添加上传图片
  onAddImage: function (params) {
    let t = this;
    t.updateComponent();
  },


  onUpdateStore: function () {
    let t = this;
  },

  onFetch: function (params, cb) {
    let t = this;
    t.updateComponent();
    cb && cb(t.data);
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }


});
